$(function () {

    var questionId = $('#questionId').val();
    var questioner = JSON.parse($('#questioner').val());
    var attender = JSON.parse($('#attender').val());
    var isFree = $('#isFree').val();

    if(questioner || attender || isFree==1){
        getQuestionReplyList(1);
    }

    $('body').on('click','#attend-btn',function () {
        isLogin(function () {
            window.location.href = baseloaction +'/web/uc/order/initPayOrder?orderType=COMMON&shopData='+questionId+'-ATTEND-1&shareKey=';
        });
    }).on('click','#submitReply',function () {//保存评论
        saveQuestionReply();
    }).on('click','.huifu',function () {//展开回复评论框
        var replyId = $(this).data('id');
        showReplyTextarea(replyId);
    }).on('click','.btn-success',function () {//保存评论回复
        var obj = $(this);
        saveCommentReply(obj);
    }).on('click','.btn-outline',function () {//取消评论回复
        cancelReply();
    }).on('click','#add-question', function () {//打开追问输入框
        $("#add-question-warning").hide();
        $('#add-question-div').show();
        $("#cancle").show();
        $("#add-question").hide();
        document.querySelector("#add-question-div").scrollIntoView({block: "end", behavior: "smooth"});
    }).on('click','#cancle', function () {//关闭追问输入框
        $('#add-question-div').hide();
        $("#cancle").hide();
        $("#add-question").show();
    }).on('click','#submitAddContext',function () {//保存追问内容
        saveQuestionAdd();
    }).on('click','#qa-ta-but-box',function () {
        var teacherId = $('#qa-ta-but-box').data('id');
        questionToTeacher(teacherId);
    }).on("keyup","#addContext",function(){
        var that =$(this);
        if(that.val().length>=200){
            that.val( that.val().substring(0,200));
            icModale(0,'',"最多可输入200个字");
        }

    }).on("keyup","textarea[name=replyContext]",function(){
        var that =$(this);
        if(that.val().length>=200){
            that.val( that.val().substring(0,200));
            icModale(0,'',"最多可输入200个字");
        }
    });

    /**
     * 加载评论列表
     * @param pageNo
     */
    function getQuestionReplyList(pageNo) {
        var params = {"questionId": questionId}
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/web/qa/reply/ajaxQueryQaReplyList',
            contextId: 'reply-list-context',
            params: params,
            pageNo: pageNo,
            pageCall: getQuestionReplyList,
            callback: function () {
                $('[data-toggle=tooltip]').tooltip();
                //$('#reply-count').html('('+ total +')');
                if($('#submitReply').prop("disabled")){
                    $('.huifu').prop('disabled',true);
                }
                lazyUserImage();
            }
        }
        getPageAjaxData(obj);
    }

    /**
     * 保存追问
     */
    function saveQuestionAdd() {
        isLogin(function () {

                var addContext = $('#addContext').val();
                if(addContext.trim().length==0){
                    $('#add-question-warning').find('i').html(' 追问内容不能为空！');
                    $("#add-question-warning").show();
                    return;
                }
                if(addContext.trim().length>200){
                    $('#add-question-warning').find('i').html(' 追问内容不能超过200个字！');
                    $("#add-question-warning").show();
                    return;
                }
                $("#add-question-warning").hide();
                var params = $("#addQuestionForm").serializeJSON();
                params = JSON.stringify(params);
                $.ajax({
                    url: baseloaction + '/web/uc/qa/add/saveQuestionAdd',
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (result) {
                        icModale(0,'',result.message);
                        if(result.success){
                            var questionAdd = result.entity;
                            var addCount =  result.addCount==1?'一':'二'
                            $(".packCourMenu__wrap").append('<div>' +
                                '<section class="packCourTitle mb20 pb10 pr">' +
                                '<span class="fs14 c-333">'+ questionAdd.addContext +'</span>' +
                                '<aside class="packCourNum">' +
                                '<button class="btn btn-info btn-rounded btn-outline btn-sm" type="button">追问'+addCount +'</button>' +
                                '</aside>' +
                                '</section>' +
                                '</div>');
                            $("#add-question-div").hide();
                            $("#cancle").hide();
                            $("#add-question").show();
                            $("#addContext").val("");
                            $("#add-question-warning").hide();
                            if(result.addCount >= 2){
                                $("#add-question").prop("disabled","disabled");
                            }
                        }
                    },
                    error: function (error) {
                        icModale(0,'','系统繁忙，请稍后重试');
                    }
                })
        })
    }

    /**
     * 向讲师提问
     * @param teacherId
     */
    function questionToTeacher(teacherId) {
        isLogin(function () {
            window.location.href = baseloaction + '/web/uc/qa/initSaveQuestionView?teacherId='+ teacherId + '&type=2';
        });
    }

    /**
     * 保存问题评论
     */
    function saveQuestionReply() {
        isLogin(function () {

                var replyContext = $("#replyContext").val();
                if(replyContext.trim().length == 0){
                    $("#warning").find('i').html('  评论内容不能为空！');
                    $("#warning").show();
                    return;
                }
                if(replyContext.trim().length>200){
                    $("#warning").find('i').html('  评论内容不能超过200个字！');
                    $("#warning").show();
                    return;
                }
                $("#warning").hide();
                var params = $("#replyForm").serializeJSON();
                params = JSON.stringify(params);
                $.ajax({
                    url: baseloaction + '/web/uc/qa/reply/saveQaReply',
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (result) {
                        icModale(0,'',result.message);
                        if(result.success){
                            $("#reply-count").html(parseInt($("#reply-count").html()) + 1);
                            getQuestionReplyList(1);
                            $("#replyContext").val('');
                        }
                    },
                    error: function (error) {
                        icModale(0,'','系统繁忙，请稍后重试');
                    }
                })

        })
    }

    /**
     * 保存评论回复
     */
    function saveCommentReply(obj) {
        isLogin(function () {

                var objDiv = obj.parents('div.comment-context');
                var replyContext = objDiv.find('textarea').val();
                if(replyContext.trim().length==0){
                    objDiv.find('i').html(' 回复内容不能为空！')
                    objDiv.find('.c-danger').show();
                    return;
                }
                if(replyContext.trim().length>200){
                    objDiv.find('i').html(' 回复内容不能超过200个字！')
                    objDiv.find('.c-danger').show();
                    return;
                }
                objDiv.find('i').hide();

                var params = objDiv.find('form').serializeJSON();
                params = JSON.stringify(params);
                $.ajax({
                    url: baseloaction + '/web/uc/qa/reply/saveQaReply',
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (result) {
                        icModale(0,'',result.message);
                        if(result.success){
                            $("#reply-count").html(parseInt($("#reply-count").html()) + 1);
                            getQuestionReplyList(1);
                        }
                    },
                    error: function (error) {
                        icModale(0,'','系统繁忙，请稍后重试');
                    }
                })

        })
    }

    /**
     * 展开评论回复框
     * @param replyId
     */
    function showReplyTextarea(replyId) {
        $('.comment-context').find('textarea').val('');
        $('.c-danger').hide();
        $('.comment-context').hide();
        $('#context-'+ replyId).show();
    }

    /**
     * 取消评论回复
     */
    function cancelReply() {
        $('.comment-context').hide();
        $('.comment-context').find('textarea').val('');
    }
    qaImgShowFn()
})

//问题主干图片放大方法
function qaImgShowFn() {
    layer.photos({
        photos: '.question-content-img__wrap',//选择器
        anim: -1,
        tab: function(pic, layero){}
    });
}